﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAEKAUDIT 
    { 
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            ZoneManager.Visibility = Visibility.Hidden;
            ZoneIT.Visibility = Visibility.Hidden;

            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count == 0)
                return;
            if (setResult.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            DataRow dwUser = setResult.Tables[0].Rows[0];
            txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
            switch (dwUser["HRBGRP"].ToString())
            {
                case "EM":
                    ZoneManager.Visibility = Visibility.Visible;
                    ZoneIT.Visibility = Visibility.Hidden;
                    ZoneManager.IsEnabled = true;
                    break;
                case "YA":
                    ZoneManager.Visibility = Visibility.Visible;
                    ZoneIT.Visibility = Visibility.Visible;
                    ZoneIT.IsEnabled = true;
                    ZoneManager.IsEnabled = true;
                    break;
            }
            expLog.IsExpanded = false;
            EventDispatcher.Instance.RaiseStatus(txtLogIn.Text, StatusEventType.UserLogIn);
        }
    
        public override void OnWorkflowIDChange(string wfid)
        {
            base.OnWorkflowIDChange(wfid);
            try
            {
                dwWFEvent = null;
                DataSet SResult = m_Data.INTEXDBC.QueryICC_WFEVENTS(wfid, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwWFEvent = row;
                }
                if (dwWFEvent == null)
                    return;
                string events = dwWFEvent["WFEVENTS"].ToString().Trim();
                switch (events)
                {
                    case "M002001"://更改供应商
                        KRS = dwWFEvent["KRS"].ToString();
                        LFD = dwWFEvent["LFD"].ToString();
                        txtLSNAME.Text = Helpers.Helper.GetGB2312Value(dwWFEvent["WHAT"].ToString().Trim());
                        string filter = "LSNAME1 LIKE '%" + txtLSNAME.Text.Trim() + "%'";
                        cvLSNAME.CustomFilter = filter;
                        btnQuery_Click(null, null);
                        break;
                    case "M002003"://其他
                        KRS = dwWFEvent["KRS"].ToString();
                        LFD = dwWFEvent["LFD"].ToString();
                        POS = dwWFEvent["POS"].ToString();
                        string ABAID = dwWFEvent["HOW"].ToString().Trim();
                        btnQuery_Click(null, null);
                        string filter1 = "EKPAUFPOS='" + POS + "'";
                        cvMaster.CustomFilter = filter1;
                        filter1 = "ABAAUFPOS='" + POS + "'";
                        cvABA.CustomFilter = filter1;
                        break;
                    case "M002002"://其他

                    case "M002004"://其他
                    case "M002005"://其他
                    case "M002006"://其他
                        KRS = dwWFEvent["KRS"].ToString();
                        LFD = dwWFEvent["LFD"].ToString();
                        btnQuery_Click(null, null);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
      
        public void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                    return;
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKPAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKPAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKPAUFPOS"] = dwEKPOS["EKPAUFPOS"];
                dwData["EKPBEKZ"] = "N";
                dwData["EKPKNTRNRL"] = m_Data.day + "." + m_Data.month + "." + Helpers.Helper.Right(m_Data.year, 2) + "       ";
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 4);
                btnQuery_Click(null, null);
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
     
     

        private void cmdThrowEKP_Click(object sender, RoutedEventArgs e)
        {
            string filter = "";
            DataRow[] rows;
            try
            {
                if (dwEKPOS == null)
                {
                    MessageBox.Show("请选择采购单!");
                    return;
                }
                string krs, lfd, pos, abaid;
                krs = dwEKPOS["EKPAUFKRS"].ToString();
                lfd = dwEKPOS["EKPAUFLFD"].ToString();
                pos = dwEKPOS["EKPAUFPOS"].ToString();

                decimal EKPME1 = Convert.ToDecimal(dwEKPOS["EKPME1"].ToString()) / 100;
                var q1 = from DataRow rx in Set4ITX.ICC_ABAUFM.AsEnumerable()
                         where rx["ABAAUFKRS"].ToString() == krs
                         && rx["ABAAUFLFD"].ToString() == lfd
                         && rx["ABAAUFPOS"].ToString() == pos
                         select rx;
                foreach (DataRow row in q1)
                {
                    row["ABAAUFTYP"] = "N";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "      ";
                    row["ABAAUFPOS"] = "   ";
                    row["ABAQTYRESERVE"] = 0;
                    RelesedQty += Convert.ToDecimal(row["ABARESMG"].ToString());
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 3);
                }
                dwEKPOS["EKPWIEBEST"] = m_Data.date;
                m_Data.INTEXDBC.UpdateEKPOS(dwEKPOS, 6);
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void cmdDeleteSingleReq_Click(object sender, RoutedEventArgs e)
        {
            string filter = "";
            DataRow[] rows;
            try
            {
                if (dwEKPOS == null)
                {
                    MessageBox.Show("请选择采购单!");
                    return;
                }

                if (dwABAUFM == null)
                {
                    MessageBox.Show("请定位需求!");
                    return;
                }
                string krs, lfd, pos, abaid;
                krs = dwABAUFM["ABAAUFKRS"].ToString();
                lfd = dwABAUFM["ABAAUFLFD"].ToString();
                pos = dwABAUFM["ABAAUFPOS"].ToString();
                abaid = dwABAUFM["ABAID"].ToString();
                decimal EKPME1 = Convert.ToDecimal(dwEKPOS["EKPME1"].ToString()) / 100;
                var q1 = from DataRow rx in Set4ITX.ICC_ABAUFM.AsEnumerable()
                         where rx["ABAID"].ToString() == abaid
                         select rx;
                foreach (DataRow row in q1)
                {
                    row["ABAAUFTYP"] = "N";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "      ";
                    row["ABAAUFPOS"] = "   ";
                    row["CONTROL"] = "APP";
                    row["ABAQTYRESERVE"] = 0;
                    RelesedQty += Convert.ToDecimal(row["ABARESMG"].ToString());
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 3);
                }
                //filter = "ABAID='" + dwABAUFM["ABAID"].ToString() + "'";
                //rows = Set4ITX.ICC_ABAUFM.Select(filter);
                decimal QTYREQ = 0;
                var q2 = from DataRow rx in Set4ITX.ICC_ABAUFM.AsEnumerable()
                         where rx["ABAAUFKRS"].ToString() == krs
                         && rx["ABAAUFLFD"].ToString() == lfd
                         && rx["ABAAUFPOS"].ToString() == pos
                         select rx;

                foreach (DataRow row in q2)
                {
                    QTYREQ += Convert.ToDecimal(row["ABARESMG"].ToString());
                }
                foreach (DataRow row in q2)
                {
                    decimal req = Convert.ToDecimal(row["ABARESMG"].ToString());
                    row["ABAQTYRESERVE"] = Math.Round((req / QTYREQ) * EKPME1);
                    row["CONTROL"] = "   ";
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 3);
                }
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void cmdChgSupplier_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                if (dwEKPOS == null)
                {
                    MessageBox.Show("请寻找一条采购单行!");
                    return;
                }
                if (MessageBox.Show("整个采购单的供应商都会被更新，确认吗?", "Yes/No", MessageBoxButton.YesNo) == MessageBoxResult.No)
                    return;
                if (dwLIEFDAT == null)
                {
                    MessageBox.Show("请必须确定供应商!");
                    return;
                }
                string liefnr = dwLIEFDAT["LSLIEFNR"].ToString();
                DataRow dwData = Set4ITX.EKKOPF.NewRow();
                dwData["EKKMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKKAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKKAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKKLIEFNR"] = dwLIEFDAT["LSLIEFNR"];
                m_Data.INTEXDBC.UpdateEKKOPF(dwData, 3);
                dwData = Set4ITX.EKPOS.NewRow();
                dwData["EKPMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKPAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKPAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKPLIEFNR"] = dwLIEFDAT["LSLIEFNR"];
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 5);
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void cmdNewReq_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                {
                    MessageBox.Show("请寻找一条采购单行!");
                    return;
                }
                if (dwEKPOS["EKPBEKZ"].ToString().Trim() == "R")
                {
                    MessageBox.Show("已经补采购，不要重复操作!");
                    return;
                }
                dwEKPOS["EKPBEKZ"] = "R";
                string filter = "ABAAUFKRS='" + dwEKPOS["EKPAUFKRS"].ToString() + "' AND ABAAUFLFD='" + dwEKPOS["EKPAUFLFD"].ToString() + "' AND ABAAUFPOS='" + dwEKPOS["EKPAUFPOS"].ToString() + "'";
                DataRow[] rows = Set4ITX.ICC_ABAUFM.Select(filter);
                foreach (DataRow row in rows)
                {
                    row["ABAID"] = 0;
                    row["ABAAUFTYP"] = "N";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "      ";
                    row["ABAAUFPOS"] = "   ";
                    //                    row["ABARESMG"] = 0;
                    row["ABAQTYRESERVE"] = 0;
                    row["ABAQTYRECEIVED"] = row["ABARESMG"];
                    m_Data.INTEXDBC.InsertICC_ABAUFM(row, 9);
                }
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void cmdDeleteEK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtDelete.Text.ToString().Trim() == "N")
                {
                    MessageBox.Show("如果你确认删除，请将标志改为Y");
                    return;
                }

                listGS08N.Text = "";
                string client = "00";
                string krs = m_Data.Right("  " + txtEKPAUFKRS.Text.ToString().Trim(), 2);
                string lfd = m_Data.Right("000000" + txtEKPAUFLFD.Text.ToString().Trim(), 6);
                string pos = m_Data.Right("000" + txtEKPAUFPOS.Text.ToString().Trim(), 3);
                m_Data.Delete(client, krs, lfd, pos);
                txtDelete.Text = "N";
                listGS08N.Text = krs + lfd + "已经成功删除！";
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

     

        private void WFAPPLY_Click(object sender, RoutedEventArgs e)
        {
            if (dwEKPOS == null)
            {
                MessageBox.Show("请选择采购单");
                return;
            }
            if (dwEvents == null)
            {
                MessageBox.Show("请选择需要申请的事件");
                return;
            }
            DataRow dwData = null;
            string msg = "";
            string EventID = dwEvents["WFEVENTS"].ToString().Trim();
            MessageInfo msgs = new MessageInfo();
            var query = from DataRow row in Set4ITX.EKKOPF.AsEnumerable()
                        where row["EKKAUFKRS"].ToString() == dwEKPOS["EKPAUFKRS"].ToString()
                        && row["EKKAUFLFD"].ToString() == dwEKPOS["EKPAUFLFD"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                dwData = row;
            }
            switch (EventID)
            {
                case "M002001":
                    if (dwLIEFDAT == null)
                    {
                        MessageBox.Show("请选择供应商");
                        return;
                    }
                    msg = Helpers.Helper.ConvertGB2312To8859P1(dwEvents["WFEDES"].ToString());
                    msgs.Where = m_Data.UserID;
                    msgs.What = Helpers.Helper.ConvertGB2312To8859P1(dwLIEFDAT["LSNAME1"].ToString());

                    break;
                case "M002003"://其他
                    if (POS.Trim() == "000")
                    {
                        MessageBox.Show("请确定采购单行");
                        return;
                    }
                    if (dwABAUFM == null)
                    {
                        MessageBox.Show("请确定需求行");
                        return;
                    }
                    msg = Helpers.Helper.ConvertGB2312To8859P1(dwEvents["WFEDES"].ToString());
                    msgs.Where = m_Data.UserID;
                    msgs.What = dwABAUFM["ABAID"].ToString();
                    dwData = dwEKPOS;
                    break;
                case "M002002"://其他
                case "M002004"://其他
                case "M002005"://其他
                case "M002006"://其他
                    msg = Helpers.Helper.ConvertGB2312To8859P1(dwEvents["WFEDES"].ToString());
                    msgs.Where = m_Data.UserID;
                    break;
            }
            m_Data.INTEXBL.CheckEvents(EventID, dwData, msg, msgs);
            if (EventID != EventID)
            {
                if (vwEvents.View != null)
                    vwEvents.View.MoveCurrentTo(-1);
                if (vwEKPOS != null)
                    vwEKPOS.View.MoveCurrentTo(-1);
            }
        }

        private void EVENTS_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb.SelectedValue == null)
                return;
            txtEvents.Text = (cb.SelectedValue).ToString();
        }

        private void btnWFConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (dwWFEvent == null)
                return;
            dwWFEvent["CONTROL"] = "OK";
            dwWFEvent["CHGUSER"] = m_Data.UserID;
            dwWFEvent["CHGTIME"] = Helpers.Helper.Left(System.DateTime.Now.ToString(), 20);
            m_Data.INTEXDBC.UpdateICC_WFEVENTS(dwWFEvent);
            cvMaster.CustomFilter = null;
            cvABA.CustomFilter = null;
        }

        private void btnWFReject_Click(object sender, RoutedEventArgs e)
        {
            if (dwWFEvent == null)
                return;
            dwWFEvent["CONTROL"] = "JJ";
            dwWFEvent["CHGUSER"] = m_Data.UserID;
            dwWFEvent["CHGTIME"] = Helpers.Helper.Left(System.DateTime.Now.ToString(), 20);
            m_Data.INTEXDBC.UpdateICC_WFEVENTS(dwWFEvent);
            cvMaster.CustomFilter = null;
            cvABA.CustomFilter = null;
        }

        private void cmdBack2EK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                    return;
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKPAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKPAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKPAUFPOS"] = dwEKPOS["EKPAUFPOS"];
                dwData["EKPBEKZ"] = "N";
                dwData["EKPKNTRNRL"] = "               ";
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 4);
                btnQuery_Click(null, null);
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void cmdBack2EKDispo_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                    return;
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKPAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKPAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKPAUFPOS"] = dwEKPOS["EKPAUFPOS"];
                dwData["EKPBEKZ"] = " ";
                dwData["EKPKNTRNRL"] = "               ";
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 4);
                btnQuery_Click(null, null);
                btnWFConfirm_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}
